package tiepbm.app.util;


import tiepbm.app.store.object.Obj_User;
import tiepbm.app.util.memory.MyPreference;
import android.content.Context;
import com.google.gson.Gson;

public class MyPreferenceManager {


	public static final String USER_NAME_RANDOM = "user_name_random";
	public static final String PASS_RANDOM = "pass_random";

	private static MyPreferenceManager instance;
	private MyPreference preference;

	private MyPreferenceManager(Context context) {
		this.preference = MyPreference.getInstance(context);
	}

	public static MyPreferenceManager getInstance(Context context) {
		if (instance == null) {
			instance = new MyPreferenceManager(context);
		}
		return instance;
	}

	/**
	 * clear values
	 */
	public void clear() {
		preference.clear();
	}


	/**
	 * Save account name and password
	 */
	public void setAccount(Obj_User account) {
		String jUser = new Gson().toJson(account);
		preference.putString("account", jUser);
	}

	/**
	 * Get account info
	 */
	public Obj_User getAccount() {
		String jUser = preference.getString("account", "");
		Obj_User account = null;
		try {
			account = new Gson().fromJson(jUser, Obj_User.class);
		} catch (Exception e) {
			Log.e(e.toString());
			return null;
		}
		return account;
	}

	/**
	 * Logout, clear account name and password
	 */
	public void clearAccount() {
		preference.putString("account", null);
	}

	public void setUserRandom(String userName) {
		preference.putString(USER_NAME_RANDOM, userName);
	}

	public String getUserRandom() {
		return preference.getString(USER_NAME_RANDOM, "");
	}

	public void setPassRandom(String pass) {
		preference.putString(PASS_RANDOM, pass);
	}

	public String getPassRandom() {
		return preference.getString(PASS_RANDOM, "");
	}
}
